All files / web/src/app/practice/[studentId]/observe ObservationClient.tsx

0% Statements 0/166
0% Branches 0/1
0% Functions 0/1
0% Lines 0/166

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                                                                                                                                                                                                                                                                                                             
'use client'

import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { useCallback, useEffect, useState } from 'react'
import { SessionObserverView } from '@/components/classroom/SessionObserverModal'
import { SubscribeButton } from '@/components/notifications/SubscribeButton'
import { PageWithNav } from '@/components/PageWithNav'
import type { ActiveSessionInfo } from '@/hooks/useClassroom'
import { css } from '../../../../../styled-system/css'

interface ObservationClientProps {
  session: ActiveSessionInfo
  observerId: string
  student: {
    name: string
    emoji: string
    color: string
  }
  studentId: string
  /** Whether the observer is a parent of the student (can share session) */
  isParent?: boolean
  /** URL to the session report (shown in banner when session has ended) */
  sessionReportUrl?: string
  /** Whether the session has ended */
  sessionEnded?: boolean
}

export function ObservationClient({
  session,
  observerId,
  student,
  studentId,
  isParent = false,
  sessionReportUrl,
  sessionEnded = false,
}: ObservationClientProps) {
  const router = useRouter()
  const [navHeight, setNavHeight] = useState(80) // Default fallback

  useEffect(() => {
    // Measure the actual nav height from the fixed header
    const measureNavHeight = () => {
      const header = document.querySelector('header')
      if (header) {
        const rect = header.getBoundingClientRect()
        // Nav top position + nav height + small margin
        const calculatedHeight = rect.top + rect.height + 16
        setNavHeight(calculatedHeight)
      }
    }

    // Measure on mount and when window resizes
    measureNavHeight()
    window.addEventListener('resize', measureNavHeight)

    // Also measure after a short delay to catch any late-rendering nav elements
    const timer = setTimeout(measureNavHeight, 100)

    return () => {
      window.removeEventListener('resize', measureNavHeight)
      clearTimeout(timer)
    }
  }, [])

  const handleExit = useCallback(() => {
    router.push(`/practice/${studentId}/dashboard`, { scroll: false })
  }, [router, studentId])

  return (
    <PageWithNav navTitle={`Observing ${student.name}`} navEmoji={student.emoji}>
      <main
        data-component="practice-observation-page"
        className={css({
          minHeight: '100vh',
          backgroundColor: 'gray.50',
          _dark: { backgroundColor: 'gray.900' },
          display: 'flex',
          flexDirection: 'column',
          boxSizing: 'border-box',
        })}
        style={{
          paddingTop: `${navHeight}px`,
        }}
      >
        {sessionEnded && sessionReportUrl && (
          <div
            data-element="session-ended-banner"
            className={css({
              bg: 'green.500',
              color: 'white',
              py: 3,
              px: 4,
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              gap: 3,
              flexWrap: 'wrap',
              textAlign: 'center',
            })}
          >
            <span className={css({ fontWeight: 'medium' })}>This practice session has ended.</span>
            <Link
              href={sessionReportUrl}
              className={css({
                display: 'inline-flex',
                alignItems: 'center',
                gap: 1,
                bg: 'white',
                color: 'green.700',
                px: 4,
                py: 1.5,
                borderRadius: 'md',
                fontWeight: 'semibold',
                fontSize: 'sm',
                transition: 'all 0.2s',
                _hover: {
                  bg: 'green.50',
                },
              })}
            >
              View Session Report
              <span aria-hidden="true">→</span>
            </Link>
          </div>
        )}
        {/* Notification subscribe banner */}
        <div
          data-element="subscribe-banner"
          className={css({
            padding: '8px 16px',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            borderBottom: '1px solid',
            borderColor: 'gray.200',
            _dark: { borderColor: 'gray.700' },
          })}
        >
          <SubscribeButton
            playerId={studentId}
            playerName={student.name}
            userId={observerId}
            variant="subtle"
          />
        </div>
        <div
          className={css({
            flex: 1,
            width: '100%',
            overflow: 'hidden',
          })}
        >
          <SessionObserverView
            session={session}
            student={student}
            observerId={observerId}
            canShare={isParent}
            onClose={handleExit}
            variant="page"
          />
        </div>
      </main>
    </PageWithNav>
  )
}